﻿using System.Threading.Tasks;
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
using SkiaSharp;
using System.Threading.Tasks;
using TabbedTemplate.Models;
using TabbedTemplate.Services;
using TabbedTemplate.Views;
using Xamarin.Forms;

namespace TabbedTemplate.ViewModels
{
    public class DrawPicPageViewModel : ViewModelBase
    {
        public MainPage MainPage =>
            _mainPage ??= Application.Current.MainPage as MainPage;

        private MainPage _mainPage;
        public SKImage Image
        {
            get => _image;
            set => Set(nameof(Image), ref _image, value);
        }

        private SKImage _image;

        /// <summary>
        /// 传递的参数
        /// </summary>
        public DrawContent DrawContent
        {
            get => _drawContent;
            set => Set(nameof(DrawContent), ref _drawContent, value);
        }

        private DrawContent _drawContent;

        private IDrawPicService _drawPicService;


        private IPickPhotoService _pickPhotoService;
        private readonly IPunchNavigationService _punchNavigationService;

        private IImageService _imageService;
        private IAlertService _alertService;

        public DrawPicPageViewModel(IDrawPicService drawPicService,
            IPickPhotoService pickPhotoService, IImageService imageService, IAlertService alertService,IPunchNavigationService punchNavigationService) {
            _punchNavigationService = punchNavigationService;
            _drawPicService = drawPicService;
            _pickPhotoService = pickPhotoService;
            _imageService = imageService;
            _alertService = alertService;
        }

        private RelayCommand _saveCommand;

        public RelayCommand SaveCommand =>
            _saveCommand ??= (_saveCommand =
                new RelayCommand(async () => await SaveCommandFunction()));

        public async Task SaveCommandFunction()
        {
            await _imageService.SaveSanpImageToAlbumAsync(Image);
            _alertService.ShowAlert("提示", "保存成功", "确定");
            var index = MainPage.Children.IndexOf(MainPage.CurrentPage);
            await MainPage.Children[index].Navigation.PopAsync();

        }

        private RelayCommand<SKEventArgsToReturn> _drawPicCommand;

        public RelayCommand<SKEventArgsToReturn> DrawPicCommand =>
            _drawPicCommand ??= (_drawPicCommand =
                new RelayCommand<SKEventArgsToReturn>(async sKEventArgsToReturn => await
                    DrawPicCommandFunction(sKEventArgsToReturn)));

        //public async Task DrawPicCommandFunction(SKEventArgsToReturn skEventArgsToReturn) =>
        //    await _drawPicService.OnCanvasViewPaintSurface(skEventArgsToReturn,
        //        DrawContent);


        //private RelayCommand _drawPicCommand;

        public async Task DrawPicCommandFunction(
            SKEventArgsToReturn skEventArgsToReturn)
        {
            Image = await _drawPicService.OnCanvasViewPaintSurface(
                skEventArgsToReturn, DrawContent);
        }


        //public async Task DrawPicCommandFunction() => _drawPicService.OnCanvasViewPaintSurface(skEventArgsToReturn, DrawContent);
    }
}
